package com.arcvideo.acsplayer.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;

public class VolumeView extends FrameLayout {
	private String TAG = VolumeView.class.getSimpleName();
	private Context mContext = null;
	private ImageButton mVolume = null;
	private VerticalSeekBar mVolumeSeekBar = null;
	private TextView mVolumePrecentTextView = null;
	private int mVolumeCurrent = 0;
	private int mVolumeMax = 0;
	private boolean mbMute = false;
	private AudioManager mAudioManager = null;

	/**
	 * 进度条事件
	 */
	private OnSeekBarChangeListener mSeekBarChangeListener = new OnSeekBarChangeListener() {
		private int mLastVolumeValue = 0;

		// 触发操作，拖动
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			if (mLastVolumeValue < progress) {
				// 增加音量，调出系统音量控制
				mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);

			} else if (mLastVolumeValue > progress) {
				mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, 0);
			}
			mLastVolumeValue = progress;
		}

		// 表示进度条刚开始拖动，开始拖动时候触发的操作
		public void onStartTrackingTouch(SeekBar seekBar) {
			mLastVolumeValue = seekBar.getProgress();
		}

		// 停止拖动时候
		public void onStopTrackingTouch(SeekBar seekBar) {
			setVolume(seekBar.getProgress());
			mbMute = false;
		}

	};

	/**
	 * 按键点击事件
	 */
	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
				case R.id.volume :
					setMute();
					break;

				default :
					break;
			}
		}
	};

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public VolumeView(Context context) {
		super(context);
		mContext = context;
		initUI();
		initData();
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 * @param attributeSet
	 */
	public VolumeView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
		initUI();
		initData();
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {
		LayoutInflater.from(mContext).inflate(R.layout.actionbar_volume, this);
		mVolume = (ImageButton) findViewById(R.id.volume);
		mVolumeSeekBar = (VerticalSeekBar) findViewById(R.id.volume_seekbar);
		// mVolumeSeekBar.setOnSeekBarChangeListener(mSeekBarChangeListener);
		mVolume.setOnClickListener(mOnClickListener);
		mVolumePrecentTextView = (TextView) findViewById(R.id.volume_precent);
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
		mVolumeMax = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		mVolumeCurrent = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mVolumeSeekBar.setMax(mVolumeMax);
		mVolumeSeekBar.setProgress(mVolumeCurrent);
		mVolumePrecentTextView.setText("" + mVolumeCurrent * 100 / mVolumeMax);
	}

	/**
	 * 显示
	 */
	public void show() {
		initData();
		setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏
	 */
	public void hide() {
		setVisibility(View.GONE);
	}

	/**
	 * 音量增加
	 */
	public void increase() {
		mVolumeCurrent++;
		if (mVolumeCurrent >= mVolumeMax)
			mVolumeCurrent = mVolumeMax;
		setVolume(mVolumeCurrent);
	}

	/**
	 * 音量减少
	 */
	public void decrease() {
		mVolumeCurrent--;
		if (mVolumeCurrent <= 0)
			mVolumeCurrent = 0;
		setVolume(mVolumeCurrent);
	}

	/**
	 * 设置音量
	 *
	 * @param value
	 */
	private void setVolume(int value) {
		if (mbMute) {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
		} else {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0); // tempVolume:音量绝对值
		}

		mVolumeSeekBar.setProgress(value);

		if (mbMute) {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
		} else {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0); // tempVolume:音量绝对值
		}

		// 更改音量文本显示
		mVolumePrecentTextView.setText("" + value * 100 / mVolumeMax);
	}

	/**
	 * 设置音量
	 *
	 * @param percent
	 */
	public void setVolume(float percent) {
		int value = (int) (percent * mVolumeMax) + mVolumeCurrent;
		if (value > mVolumeMax)
			value = mVolumeMax;
		else if (value < 0)
			value = 0;

		if (mbMute) {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
		} else {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0); // tempVolume:音量绝对值
		}

		mVolumeSeekBar.setProgress(value);
		// 更改音量文本显示
		mVolumePrecentTextView.setText("" + value * 100 / mVolumeMax);
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
	}

	/**
	 * 设置静音
	 */
	public void setMute() {
		mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, mbMute);
		mbMute = !mbMute;
	}
}